<template>
  <div class="app-container">

    <el-col :span="16">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="学号" prop="sno">
          <el-input
            v-model="queryParams.sno"
            placeholder="请输入学号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:student:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:student:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:student:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:student:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="id" />
        <el-table-column label="姓名" align="center" prop="name" />
        <el-table-column label="学号" align="center" prop="sno" />
        <el-table-column label="性别" align="center" prop="sex" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:student:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:student:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-col>

    <el-col :span="8" style="background-color: aliceblue;">
      <div style="height: 500px;overflow: scroll">
        <el-timeline style="max-width: 100%">
          <el-timeline-item
            v-for="(activity, index) in activities"
            :key="index"

            :color="activity.color"

            :timestamp="activity.timestamp"
          >
            {{ activity.content }}
          </el-timeline-item>
        </el-timeline>
      </div>
      <div id="container">
        <div id="chat">
          <el-input
            v-model="msg"
            input-style="width: 100%;height:50px"
            :rows="2"
            type="text"
            placeholder="Please input"
            @keydown.enter="sendMsg();"
          />
          <!-- 按钮在输入框右侧 -->
          <el-button
            style="position: absolute;float: right;margin-left: -70px"

            @click="sendMsg()">发送</el-button>
        </div>
      </div>
    </el-col>

  </div>
</template>

<script>
import { listStudent, delStudent } from "@/api/system/student"
//import { MoreFilled } from '@element-plus/icons-vue'
import {ref} from "vue";
//ref 是创建响应式数据的函数
export default {
  name: "Student",
  //setup函数: 组件的初始化逻辑


  data() {
    return {
      activities:[
        {
          content: '⭐欢迎来到哈信息学生管理平台！请问有什么可以帮您的?',
          timestamp: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
          color: '#0bbd87',
        },
      ],
      msg: null,
      eventSource : null,
      count : 2,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生管理表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sno: null,
        sex: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        sno: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  activated() {
    // 每次激活组件时重新加载数据
    this.getList();
  },
  methods: {
    sendMsg() {
      let mg =this.msg;
      if (this.eventSource) {
        this.eventSource.close();
      }
      this.activities.push(
        {
          content: `你:`+mg,
          timestamp: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
          size: 'large',
          type: 'primary'
        },
      );
      this.activities.push(
        {
          content: 'waiting...',
          timestamp: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
          color: '#0bbd87',
        },
      );
      // sse: 服务端推送 Server-Sent Events
      this.eventSource = new EventSource(`http://localhost:8080/ai/generateStreamAsString?message=`+mg);

      this.msg='';
      this.eventSource.onmessage = (event) => {
        console.log(event)
        if (event.data === '[complete]') {
          this.count = this.count + 2;
          this.eventSource.close();

          this.getList();// 每次对话完后刷新列表
         // this.$router.push({ path: 'stuAdd' })
          return;
        }
        this.activities[this.count].content += event.data;
      };
      this.eventSource.onopen = (event) => {
        this.activities[this.count].content = '';
      };
    },
    /** 查询学生管理列表 */
    getList() {

      this.loading = true
      listStudent(this.queryParams).then(response => {
        console.log(response.rows)
        this.studentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sno: null,
        sex: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = false
      //跳转到add.vue
      this.$router.push({ path: 'stuAdd' })
     // this.title = "添加学生管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      this.$router.push({ path: 'editStu' ,query: { id: id }})
      // getStudent(id).then(response => {
      //   this.form = response.data
      //   this.open = true
      //   this.title = "修改学生管理"
      // })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除学生管理编号为"' + ids + '"的数据项？').then(function() {
        return delStudent(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>



